using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ample.BLL.Store;
using Ample.BLL.User;
using Ample.Model;

public partial class Users_SalesTeams : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            BindData();
    }

    private void BindData()
    {
        var orderBO = new OrderBO();
        IEnumerable<Order> unrefOrders = orderBO.GetOrderByUserName(this.Profile.UserName).Where(o => o.BoardUser == null);

        if(unrefOrders.Count() == 0)
            Response.Redirect("~/Users/SalesTeamsDetails.aspx");

        var boardBO = new BoardBO();

        IEnumerable<BoardPosition> boardPositions = boardBO.GetPositionsByLevel(EnumBoardType.Consultant.ToString());
        IEnumerable<Board> boardsOpen = boardBO.GetPositionsByOpen(true);
        IEnumerable<Board> boardsClosed = boardBO.GetPositionsByOpen(false);

        ddlPurchase.DataSource = unrefOrders;
        ddlPurchase.DataTextFormatString = "Order No: {0}";
        ddlPurchase.DataTextField = "OrderId";
        ddlPurchase.DataValueField = "OrderId";
        ddlPurchase.DataBind();
        
        ddlOpenBoard.DataSource = boardsOpen;
        ddlOpenBoard.DataTextField = "BoardCode";
        ddlOpenBoard.DataValueField = "BoardId";
        ddlOpenBoard.DataBind();

        ddlSalesTeam.DataSource = boardsClosed;
        ddlSalesTeam.DataTextField = "BoardCode";
        ddlSalesTeam.DataValueField = "BoardId";
        ddlSalesTeam.DataBind();

        ddlFirstPosChoice.DataSource = boardPositions.Where( b => b.PositionCode.Contains("Red"));
        ddlFirstPosChoice.DataTextField = "PositionCode";
        ddlFirstPosChoice.DataValueField = "PositionCode";
        ddlFirstPosChoice.DataBind();

        ddlSecPosChoice.DataSource = boardPositions.Where(b => b.PositionCode.Contains("Red"));
        ddlSecPosChoice.DataTextField = "PositionCode";
        ddlSecPosChoice.DataValueField = "PositionCode";
        ddlSecPosChoice.DataBind();

        //var userBO = new UserBO();

        //UserProfile userProfile = userBO.GetUserProfileByUsername(this.Profile.UserName);
        //txtRefDistName.Text = userProfile.SponsorName ?? -;
        //txtRefDistID.Text = userProfile.SponsorID;
    }

    protected void btnJoin_Click(object sender, EventArgs e)
    {
        if(chkTerms.Checked)
        {
            var userBO = new UserBO();
            User user = userBO.GetUserByUsername(txtRefDistID.Text.Trim());

            if (user != null)
            {
                var boardBO = new BoardBO();

                int selectedBoardId = -1;

                if (Int32.Parse(ddlOpenBoard.SelectedValue) != selectedBoardId)
                    selectedBoardId = Int32.Parse(ddlOpenBoard.SelectedValue);
                else if (Int32.Parse(ddlPurchase.SelectedValue) != selectedBoardId)
                    selectedBoardId = Int32.Parse(ddlSalesTeam.SelectedValue);
                
                var boardUsers = boardBO.GetBoardUsersByBoardId(selectedBoardId);

                var yellowBoardUsers =
                    boardUsers.Where(b => b.PositionCode.Contains(EnumPositionCodeType.Yellow.ToString()));

                BoardUser validParentBoardUser = null;
                foreach (var yellowBoardUser in yellowBoardUsers)
                    foreach (var order in user.Orders)
                        if(order.OrderId == yellowBoardUser.OrderId)
                            validParentBoardUser = yellowBoardUser; 
                
                //var validParentBoardUser = yellowBoardUsers.Where(y => user.Orders.Contains(y.Order)).SingleOrDefault();

                if (validParentBoardUser != null)
                {
                    var boardUser = new BoardUser
                                        {
                                            BoardId = selectedBoardId,
                                            OrderId = Int32.Parse(ddlPurchase.SelectedValue),
                                            PositionCode =
                                                string.Format("{0};{1}", ddlFirstPosChoice.SelectedValue.Trim(),
                                                              ddlSecPosChoice.SelectedValue.Trim()),
                                            StartDate = DateTime.Now,
                                            Approved = false,
                                            UserId = user.UserId,
                                            ParentBoardUserId = validParentBoardUser.BoardUserId
                                        };

                    boardBO.SaveBoardUser(boardUser);

                    Response.Redirect("~/Users/SalesTeamsDetails.aspx");
                }
                else
                {
                    lblResult.Text = "There is no referral by this ID in the selected board.";
                    lblResult.Visible = true;
                }
            }
            else
            {
                lblResult.Text = "There is no referral by this ID in the selected board.";
                lblResult.Visible = true;
            }
        }
        
    }
}
